package com.example.animalcase;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.*;
import android.widget.TextView;
import android.widget.Toast;
import com.example.animalcase.bean.Note;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class Read2Fragment extends Fragment {

    private RecyclerView mRecyclerView;
    private List<Note> mNotes;
    private NoteDbOpenHelper mNoteDbOpenHelper;
    private MyAdapter myAdapter;


    private OnFragmentInteractionListener mListener;



    public Read2Fragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onResume() {
        super.onResume();
        refreshDataFromDb();
    }

    private void refreshDataFromDb() {
        mNotes=getDataFromDB();
        myAdapter.refreshData(mNotes);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v= inflater.inflate(R.layout.fragment_read2, container, false);

        mRecyclerView=(RecyclerView)v.findViewById(R.id.rlview);
        initData();
        myAdapter=new MyAdapter(getActivity(),mNotes);
        mRecyclerView.setAdapter(myAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));


        return v;
    }

    private void initData() {
        mNotes=new ArrayList<>();
        mNoteDbOpenHelper=new NoteDbOpenHelper(getActivity());

    }
    private List<Note> getDataFromDB(){
        return mNoteDbOpenHelper.queryAllFtomDb();

    }
    private String getTime(){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("YYYY年MM月dd HH:mm:ss");
        Date date=new Date();
        return simpleDateFormat.format(date);

    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView mTvTitle;
        TextView mTvContent;
        TextView mTvTime;
        ViewGroup rlContainer;

        public MyViewHolder(View itemView) {
            super(itemView);
            this.mTvTitle=(TextView) itemView.findViewById(R.id.tv_title);
            this.mTvContent=(TextView) itemView.findViewById(R.id.tv_content);
            this.mTvTime=(TextView) itemView.findViewById(R.id.tv_time);
            this.rlContainer=(ViewGroup) itemView.findViewById(R.id.rl_item_container);
        }
    }
    private class MyAdapter extends RecyclerView.Adapter<MyViewHolder>{
        private List<Note> mNoteList;
        private Context mContent;
        public MyAdapter(Context mContent, List<Note> mNoteList) {
            this.mNoteList = mNoteList;
            this.mContent = mContent;
        }
        @Override
        public int getItemCount() {
            return mNoteList.size();
        }

        public void refreshData(List<Note> notes){
            this.mNoteList=notes;
            notifyDataSetChanged();
        }

        public void removeData(int pos){
            mNoteList.remove(pos);
            notifyItemRemoved(pos);
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = View.inflate(mContent,R.layout.list_item_layout,null);
            MyViewHolder myViewHolder=new MyViewHolder(view);
            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, final int position) {

            final Note note=mNoteList.get(position);
            holder.mTvTitle.setText(note.getTitle());
            holder.mTvContent.setText(note.getContent());
            holder.mTvTime.setText(note.getCreatedTime());
            holder.rlContainer.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(mContent, EditActivity.class);
                    intent.putExtra("note",note);
                    mContent.startActivity(intent);
                }
            });
            holder.rlContainer.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    final Dialog dialog=new Dialog(mContent,R.style.ThemeOverlay_AppCompat_Dialog_Alert);
                                    View view= View.inflate(mContent,R.layout.dialog_layout,null);
                                    TextView dlDelete =(TextView) view.findViewById(R.id.dialog_delete);
                                    dlDelete.setGravity(Gravity.CENTER);
                                    TextView dlEdit =(TextView) view.findViewById(R.id.dialog_edit);
                                    dlEdit.setGravity(Gravity.CENTER);
                                    dlDelete.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                            int row=mNoteDbOpenHelper.deteleFromDbById(note.getId());
                            if (row>0){
                                removeData(position);

                                Toast.makeText(mContent,"删除成功！",Toast.LENGTH_SHORT).show();
                            }else {
                                Toast.makeText(mContent,"删除失败！",Toast.LENGTH_SHORT).show();
                            }
                            dialog.dismiss();
                        }
                    });
                    dlEdit.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent=new Intent(mContent, EditActivity.class);
                            intent.putExtra("note",note);
                            mContent.startActivity(intent);
                            dialog.dismiss();
                        }
                    });
                    dialog.setContentView(view);
                    dialog.show();

                    return false;
                }
            });

        }

    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }


    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}
